# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
import os
from pymongo import MongoClient
from utils.HttpUtils import HttpUtils


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
https://www.yelp.com/
"""


class Yelp_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        # res.cookies.get_dict()  # 获取cookies
        self.cookie = cookiejar.CookieJar()
        # self.ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        # self.ua = UserAgent(verify_ssl=False)  #
        self.headers = {
            # 'User-Agent': self.ua.random,

        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'

        }

    # 首页信息
    def get_contents(self, cursor):
        postData = {
            'find_desc': 'vape shop',
            'find_loc': 'Southern California, CA',
            'start': '0',
            'parent_request_id': '1fb1b96118234ef2',
            'request_origin': 'user'
        }
        url = f"https://www.yelp.com/search/snippet"

        html = HttpUtils.do_request("GET", url, self.headers, postData)

        data_json = json.loads(html.text)

        for d in data_json['searchPageProps']['searchResultsProps']['searchResults']:
            if d.get('searchResultBusiness') is not None:
                dict_data = dict()
                url_child = "https://biz.yelp.com" + d['searchResultBusiness']['businessUrl']
                dict_data['bizId'] = d['bizId']  # id
                dict_data['name'] = d['searchResultBusiness']['name']  # 名称
                dict_data['phone'] = d['searchResultBusiness']['phone']  # 电话
                dict_data['reviewCount'] = d['searchResultBusiness']['reviewCount']  # 评分
                dict_data['formattedAddress'] = d['searchResultBusiness']['formattedAddress']  # 地址
                dict_data['text'] = d['snippet']['text']  # 简介
                if d.get('searchResultBusinessHighlights') is not None:
                    if d.get('searchResultBusinessHighlights').get('businessHighlights') is not None:
                        tag_num = 1
                        for d_c in d['searchResultBusinessHighlights']['businessHighlights']:
                            dict_data['tag_' + str(tag_num)] = d_c['title']
                            tag_num += 1

                print(dict_data)

    def get_comments(self, cursor, bizId):
        postData = {
            'rl': 'en',
            'sort_by': 'relevance_desc',
            'q':'',
            'start': cursor  # '20',
        }
        # url = f"https://www.yelp.com/biz/lUPwtj3LAvroAVawYubbUg/review_feed"
        url = f"https://www.yelp.com/biz/{bizId}/review_feed"

        html = HttpUtils.do_request("GET", url, self.headers, postData)

        data_json = json.loads(html.text)

        for d in data_json['reviews']:
            dict_data = dict()
            dict_data['text'] = d['comment']['text']  # 评论内容
            dict_data['user'] = d['user']['altText']  # 用户名
            dict_data['friendCount'] = d['user']['friendCount']  # 好友数量
            dict_data['reviewCount'] = d['user']['reviewCount']  # 评论数量
            dict_data['photoCount'] = d['user']['photoCount']  # 图片数量
            dict_data['rating'] = d['rating']  # 评分
            dict_data['localizedDate'] = d['localizedDate']  # 评论时间

            # 图片链接
            tag_num = 1
            for p in d['photos']:
                dict_data['pic_url_' + str(tag_num)] = p['src']  # 图片链接
                tag_num += 1


if __name__ == '__main__':
    # conn = MongoClient('127.0.0.1', 27017)
    # db = conn["Test"]
    yelp_get = Yelp_Get()
    # 获取首页信息
    for i in range(0, 100):
        print(f"============第【{i}】页=============")
        yelp_get.get_contents(i)

    # # 获取评论内容
    # pageNum = 6  # 总页数
    # for ii in range(0, pageNum * 20, 20):
    #     yelp_get.get_comments(ii, "lUPwtj3LAvroAVawYubbUg")
